package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String hot(Model model){
        List<Region> regions = regionService.queryHotRegions();
        model.addAttribute("hotRegions",regions);
        return "destination/index";
    }
    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){
        List<Destination> dest = destinationService.getDestByRegionId(regionId);
        List<Destination> leftDests = new ArrayList<>();
        List<Destination> rightDests = new ArrayList<>();
        if (dest!=null && dest.size()>0){
            int min=dest.size()/2;
            for (int i=0;i<dest.size();i++){
                if(i<min){
                    leftDests.add(dest.get(i));
                }else {
                    rightDests.add(dest.get(i));
                }
            }
        }
        model.addAttribute("leftDests",leftDests);
        model.addAttribute("rightDests",rightDests);
        model.addAttribute("regionId",regionId);
        return "destination/hotdestTpl";
    }
    //查看某个目的地的明细
    @RequestMapping("/guide")
    public Object guide(Model model, Long id){
        List<Destination> toasts = destinationService.getToasts(id);
        //中国  广东
        Destination dest = toasts.remove(toasts.size() - 1);
        //toasts
        model.addAttribute("toasts", toasts);
        //dest  广州
        model.addAttribute("dest", dest);

        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(id);
        model.addAttribute("catalogs",catalogs);
        //strategyDetails 查询前三篇攻略
        List<StrategyDetail>strategyDetails=strategyDetailService.getViewnumTop3(dest.getId());
        model.addAttribute("strategyDetails",strategyDetails);
        return "destination/guide";
    }
    @RequestMapping("/surveyPage")
    public Object surveyPage(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //中国
        Destination dest = toasts.remove(toasts.size() - 1);
        //toasts
        model.addAttribute("toasts", toasts);
        //dest  广州
        model.addAttribute("dest", dest);
        return "destination/survey";
    }
    @RequestMapping("/survey")
    public Object detail(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        //catalogs 分类集合
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        //catalog  某个分类
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog);
        //detail 选中分类下的第一个明细
        List<StrategyDetail> details = catalog.getDetails();
        if (details!=null && details.size()>0){
           //直接取第一个明细
            StrategyDetail detail = details.get(0);
            StrategyContent content = strategyDetailService.getContent(detail.getId());
            detail.setStrategyContent(content);
            model.addAttribute("detail",detail);
        }
        return "destination/surveyTpl";
    }
    @RequestMapping("/travels")
    public Object detail(Model model, @ModelAttribute("qo")TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }
}
